//
//  LSZTotalRequest.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/13.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZTotalRequest.h"

//#import "RequestOfData.h"

@implementation LSZTotalRequest
//get  不加密
+(void)initWithGetNoEncryptionRequest :(NSString *)str :(NSMutableDictionary *)dic :(HTTPGet)block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *result = NullStringjudgment([defaults objectForKey:@"token"]);
    [manager.requestSerializer setValue:result forHTTPHeaderField:@"Auth-Token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",URLTotal,str];
    NSString * encodingString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager GET:encodingString parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        block(YES,@"",jsonObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * str = [self ErrorMessageProcessing:error];
        block(NO,str,nil);
    }];
}
//post不加密
+(void)initWithPostNoEncryptionRequest :(NSString *)str :(NSMutableDictionary *)dic :(HTTPGet)block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *result = NullStringjudgment([defaults objectForKey:@"token"]);
    [manager.requestSerializer setValue:result forHTTPHeaderField:@"Auth-Token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",URLTotal,str];
    NSString * encodingString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager POST:encodingString parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        block(YES,@"",jsonObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * str = [self ErrorMessageProcessing:error];
        block(NO,str,nil);
    }];
}
//返回字符串
+(void)initWithGetNoEncryptionRequestReturnStr :(NSString *)str :(NSMutableDictionary *)dic :(HTTPGet)block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *result = NullStringjudgment([defaults objectForKey:@"token"]);
    [manager.requestSerializer setValue:result forHTTPHeaderField:@"Auth-Token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",URLTotal,str];
    NSString * encodingString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager GET:encodingString parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *result =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        block(YES,@"",result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * str = [self ErrorMessageProcessing:error];
        block(NO,str,nil);
    }];
}
+(void)initWithPostNoEncryptionRequestReturnStr :(NSString *)str :(NSMutableDictionary *)dic :(HTTPGet)block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *result = NullStringjudgment([defaults objectForKey:@"token"]);
    [manager.requestSerializer setValue:result forHTTPHeaderField:@"Auth-Token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",URLTotal,str];
    NSString * encodingString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager POST:encodingString parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *result =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        block(YES,@"成功",result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * str = [self ErrorMessageProcessing:error];
        block(NO,str,nil);
    }];
}
#pragma mark - 三会一课的发布
+(void)initWithPostNoEncryptionRequestThreeLesson:(NSString *)str :(NSMutableDictionary *)dic :(HTTPGet)block
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString * result = NullStringjudgment([defaults objectForKey:@"token"]);
    [manager.requestSerializer setValue:result forHTTPHeaderField:@"Auth-Token"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"content-type"];
    NSString * url = [NSString stringWithFormat:@"%@%@",URLTotal,str];
    NSString * encodingString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager POST:encodingString parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@>>>>responseObject",responseObject);
        id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        block(YES,@"成功",jsonObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * str = [self ErrorMessageProcessing:error];
        block(NO,str,result);
    }];
}
+(NSString *)ErrorMessageProcessing:(NSError *)error
{
    NSString * str = [NSString stringWithFormat:@"%@",error.userInfo[@"NSLocalizedDescription"]];
    NSArray *array = [str componentsSeparatedByString:@" "]; //从字符A中分隔成2个元素的数组
    NSInteger count = array.count -1;
    NSString * strContent = array[count];
    NSLog(@"%@",strContent);
    strContent = [strContent stringByReplacingOccurrencesOfString:@"(" withString:@""];
    strContent = [strContent stringByReplacingOccurrencesOfString:@")"withString:@""];
    NSString * string = [[NSString alloc]init];
    if ([strContent isEqualToString:@"403"]) {
        string = @"重复登录";
    }else if ( [strContent isEqualToString:@"400"]){
        string = @"参数返回错误";
    }else if ([strContent isEqualToString:@"401"] ){
        string = @"登录返回错误";
    }else if ( [strContent isEqualToString:@"404"]){
        string = @"路径错误";
    }else if ( [strContent isEqualToString:@"500"]){
        string = @"未知错误";
    }else{
        string = strContent;
    }
    return string;
}
@end
